function Map(){

    this.map = null;
    this.mask = null;
    this.boundary = [];
    this.viewport = null;
    this.zoom = 10;
}

var g_map = new Map();

Map.prototype.convertFarmer = function(data) {

    var result = [];

    for (var i = 0; i < data.length; i++) {

        if(data[i].enterprise == 0){
                if(0 == data[i].poor){
                    result.push({
                    name: data[i].farmer,
                    value: data[i].coordinate.concat([data[i].sheep, data[i].district, data[i].address])
                });
            }
        }
    }

    return result;
};

Map.prototype.convertPoorFarmer = function(data) {

    var result = [];

    for (var i = 0; i < data.length; i++) {

        if(data[i].enterprise == 0){
            if(1 == data[i].poor){
                result.push({
                    name: data[i].farmer,
                    value: data[i].coordinate.concat([data[i].sheep, data[i].district, data[i].address])
                });
            }
        }
    }

    return result;
};

Map.prototype.convertEnterprise = function(data) {

    var result = [];

    for (var i = 0; i < data.length; i++) {

        if(data[i].enterprise == 1){
            result.push({
                name: data[i].farmer,
                value: data[i].coordinate.concat([data[i].sheep, data[i].district, data[i].address])
            });
        }
    }

    return result;
};


Map.prototype.getBoundaries = function(district, callback){

    var boundary = new BMap.Boundary();
    boundary.get(district, function(result){
        var boundaries = result.boundaries;

        callback(boundaries);
    });
}

Map.prototype.renderMask = function(params, api) {

    var coords = [];

    var points = g_map.mask.split(';');

    for(var i = 0; i< points.length; i++){

        var point = points[i];
        point = point.split(',');
        coords.push([point[0], point[1]]);
    }

    var points = [];
    for (var i = 0; i < coords.length; i++) {
        points.push(api.coord(coords[i]));
    }

    return {
        type: 'polygon',
        shape: {
            points: echarts.graphic.clipPointsByRect(points, {
                x: params.coordSys.x,
                y: params.coordSys.y,
                width: params.coordSys.width,
                height: params.coordSys.height
            })
        },
        style: api.style({
            fill: '#000',
            stroke: '#000',
            opacity:0.8
        })
    };
}

Map.prototype.render = function(dom_id, data){

    this.chart = echarts.init(document.getElementById('map'));

    var thiz = this;
    window.onresize = function(e){

        thiz.chart.resize();
    }

    this.getBoundaries('甘肃省庆阳市合水县',  function(boundaries){

        var EN_JW = '180,90;';
        var NW_JW = '-180,90;';
        var WS_JW = '-180,-90;';
        var SE_JW = '180,-90;';
        

        var count = boundaries.length;

        for(var i = 0; i < count; i++){
            var temp = boundaries[i];
            temp = temp.split(';');
            temp.push(temp[0]);
            boundaries[i] = temp.join(';');

            g_map.viewport = new BMap.Polygon(boundaries[i]);
        }

        g_map.mask = boundaries[0];

        g_map.mask += ';' + SE_JW + SE_JW + WS_JW + NW_JW + EN_JW + SE_JW;
        g_map.mask = g_map.mask.substr(0, g_map.mask.length - 1);

        var count = boundaries.length;

        for(var i = 0; i < count; i++){
            var temp = boundaries[i];
            temp = temp.split(';');

            for(var j = 0; j < temp.length; j++){
                var coord = temp[j];
                coord = coord.split(',');

                g_map.boundary.push([parseFloat(coord[0]), parseFloat(coord[1])]);
            }

        }

        g_map.renderChart();
    });
}

Map.prototype.renderChart = function(){

    this.chart.setOption({
        legend: {
            top:'10%',
            textStyle:{
                color:'#00CCFF'
            },
            data:['养殖场','养殖户(非贫困)','养殖户(贫困)']
        },
        title: {
            text: '合水县奶羊养殖分布',
            left: 'center',
            textStyle: {
                color: 'rgba(0,150,252,1)'
            }
        },
        tooltip : {
            trigger: 'item'
        },
        animation: false,
        bmap: {
            center: [108.28860866803551, 36.044089697751275],
            zoom: this.zoom,
            roam: true,
            mapStyleV2:{
                styleId:'473435ae45779972e51748f5a36d784e'
            }
        },
        series: [
            {
                name:'养殖户(非贫困)',
                type: 'scatter',
                coordinateSystem: 'bmap',
                data: this.convertFarmer(farmers),
                symbolSize: 10,
                encode: {
                    value: 2
                },
                label: {
                    formatter: '{b}',
                    position: 'right',
                    show: false
                },
                itemStyle: {
                    color: '#EAC635'
                },
                tooltip:{
                    formatter: function(params, ticket, callback){

                        
                        let res = `<div style="width:110%;height:30%;border-bottom:3px solid rgba(6, 127, 149, 1);color:#fff;text-align:center;padding:0;margin:0">${params.data.value[3]}</div>`;
                        // formatter += params.data.value[3] + '<br />';
                        var formatter = '';
                        formatter += '养殖户名称: ' + params.data.name + '<br />';
                        formatter += '登记奶羊数量: ' + params.value[2] + '<br />';
                        formatter += '详细地址: ' + params.value[4] + '<br />';

                        return res +  formatter;
                    },
                    extraCssText:'width:30%;height:25%;;background:rgba(6,44,94,1);border:3px solid rgba(6, 127, 149, 1);box-shadow:0px 2px 0px 0px rgba(156,158,161,1);border-radius:10px;text-align:center;'
                },
                emphasis: {
                    label: {
                        show: true
                    }
                },
                zlevel: 500
            },
            {
                name:'养殖户(贫困)',
                type: 'scatter',
                coordinateSystem: 'bmap',
                data: this.convertPoorFarmer(farmers),
                symbolSize: 10,
                encode: {
                    value: 2
                },
                label: {
                    formatter: '{b}',
                    position: 'right',
                    show: false
                },
                itemStyle: {
                    color: '#00EAFF'
                },
                tooltip:{
                    formatter: function(params, ticket, callback){

                        let res = `<div style="width:100%;height:30%;border-bottom:3px solid rgba(6, 127, 149, 1);color:#fff;text-align:center;padding:0;margin:0">${params.data.value[3]}</div>`;
                        var formatter = '';
                        formatter += '养殖户名称: ' + params.data.name + '<br />';
                        formatter += '登记奶羊数量: ' + params.value[2] + '<br />';
                        formatter += '详细地址: ' + params.value[4] + '<br />';
                        // formatter += params.data.value[3] + '<br />';
                        console.log(res);
                        return res + formatter;
                        
                        
                        // return res;
                        
                        
                    },
                    extraCssText:'width:30%;height:25%;;background:rgba(6,44,94,1);border:3px solid rgba(6, 127, 149, 1);box-shadow:0px 2px 0px 0px rgba(156,158,161,1);border-radius:10px;color:#40E6FE'
                },
                emphasis: {
                    label: {
                        show: true
                    }
                },
                zlevel: 500
            },
            {
                name:'养殖场',
                type: 'effectScatter',
                coordinateSystem: 'bmap',
                data: this.convertEnterprise(farmers),
                symbolSize: function (value) {

                    var result = 10;
                    var sheep = value[2];
                    var ranges = [
                        {
                            size:20,
                            range:[100, 499]
                        },
                        {
                            size:30,
                            range:[500, 999]
                        },
                        {
                            size:40,
                            range:[1000, 9999]
                        }
                    ];
                    if(sheep > 10000){
                        result = 50;
                    }
                    else{

                        for(var i = 0; i < ranges.length; i++){
                            var range = ranges[i].range;
                            if(sheep >= range[0] && sheep <= range[1]){
                                result = range.size;
                                break;
                            }
                        }
                    }

                    return result;
                },
                encode: {
                    value: 2
                },
                label: {
                    formatter: '{b}',
                    position: 'right',
                    show: true
                },
                itemStyle: {
                    color: '#FF3E3D'
                },
                tooltip:{
                    formatter: function(params, ticket, callback){

                        var formatter = '';

                        let res = `<div style="width:100%;height:30%;border-bottom:3px solid rgba(6, 127, 149, 1);color:#fff;text-align:center;padding:0;margin:0">${params.data.value[3]}</div>`;
                        formatter += '养殖场名称: ' + params.data.name + '<br />';
                        formatter += '登记奶羊数量: ' + params.value[2] + '<br />';
                        formatter += '详细地址: ' + params.value[4] + '<br />';

                        return res + formatter;
                    },
                    extraCssText:'width:30%;height:25%;background:rgba(6,44,94,1);border:3px solid rgba(6, 127, 149, 1);box-shadow:0px 2px 0px 0px rgba(156,158,161,1);border-radius:10px;color:#40E6FE'

                },
                emphasis: {
                    label: {
                        show: true
                    }
                },
                zlevel: 1000
            },
            {
                type: 'custom',
                coordinateSystem: 'bmap',
                renderItem: this.renderMask,
                itemStyle: {
                    opacity: 1
                },
                animation: false,
                silent: true,
                data: [0],
                z: 1
            },
            {
                type: 'lines',
                coordinateSystem: 'bmap',
                data: [
                    {
                        coords:this.boundary,
                    },
                ],
                polyline:true,
                lineStyle: {
                    opacity: 0.2,
                    width: 2,
                    color:'#FFFFFF'
                },
                animation: false,
                silent: true,
                effect:{
                    //symbol:'image://logo.png',
                    period: 30,
                    color:'green',
                    symbolSize:[6, 6],
                    trailLength:0.8,
                    show:true,
                },
                z: 2
            }
        ]
    });

    this.map = this.chart.getModel().getComponent('bmap').getBMap();
    this.map.disableDragging();
    this.map.disableScrollWheelZoom();
    this.map.disableDoubleClickZoom();
    this.map.disableDoubleClickZoom();
    this.map.setMinZoom(this.zoom);
    this.map.setMaxZoom(this.zoom);
}